package com.stylefeng.guns.modular.system.task;

import java.util.Date;
import java.util.Hashtable;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.stylefeng.guns.core.common.constant.state.KpMethod;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.core.util.HttpsClient;
import com.stylefeng.guns.modular.system.constants.ImportContants;
import com.stylefeng.guns.modular.system.dao.GpcDetailMapper;
import com.stylefeng.guns.modular.system.dao.GpcMapper;
import com.stylefeng.guns.modular.system.dao.ParamsMapper;
import com.stylefeng.guns.modular.system.model.GpcDetail;
import com.stylefeng.guns.modular.system.model.GpcStatData;
import com.stylefeng.guns.modular.system.model.Params;

@Service
public class GpcStatImportTask{

	@Autowired
	private GpcMapper gpcMapper;
	
	@Autowired
	private GpcDetailMapper gpcDetailMapper;
	
	@Autowired
	private ParamsMapper paramsMapper;
	
	public void execute(boolean isHoliday) {
		if(isHoliday){
			Params params = new Params();
			params.setgUserid("387065");
			params = paramsMapper.selectOne(params);
			if(params == null){
				return;
			}
			Hashtable<String, String> paramsMaps = new Hashtable<String, String>();
			paramsMaps.put("a", KpMethod.J.getMethod());
			paramsMaps.put("c", "StockRanking");
			paramsMaps.put("Date", DateUtil.format(new Date(), "yyyy-MM-dd"));
			paramsMaps.put("Index", "0");
			paramsMaps.put("Order", "0");
			paramsMaps.put("REnd", "");
			paramsMaps.put("RStart", "");
			paramsMaps.put("st", "100");
			paramsMaps.put("Tag", "zf");
			paramsMaps.put("Token", params.getgToken());
			paramsMaps.put("UserID", params.getgUserid());
			String result = HttpsClient.postSSL(ImportContants.getKpDataPathStr(), paramsMaps,true);
			if(StringUtils.isNotEmpty(result)){
				List<GpcStatData> gpStatDatas  = JSON.parseArray(JSON.parseObject(result).getString("list"),GpcStatData.class);
				for(GpcStatData g : gpStatDatas){
					GpcDetail gpcDetail = new GpcDetail();
					gpcDetail.setgScode(g.getCode());
					gpcDetail.setgDate(new Date());
					gpcDetail = gpcDetailMapper.selectByDate(gpcDetail);
					if(gpcDetail == null){
						gpcDetail = new GpcDetail();
						gpcDetail.setgScode(g.getCode());
						gpcDetail.setgDate(new Date());
						gpcDetail.setgBuy(g.getBuy());
						gpcDetail.setgPrice(g.getPrice());
						gpcDetail.setgRate(g.getRate());
						gpcDetail.setgRatio(g.getRatio());
						gpcDetail.setgSell(g.getSell());
						gpcDetail.setgZlje(g.getZLJE());
						gpcDetailMapper.insert(gpcDetail);
					}else{
						gpcDetail.setgBuy(g.getBuy());
						gpcDetail.setgPrice(g.getPrice());
						gpcDetail.setgRate(g.getRate());
						gpcDetail.setgRatio(g.getRatio());
						gpcDetail.setgSell(g.getSell());
						gpcDetail.setgZlje(g.getZLJE());
						gpcDetailMapper.updateById(gpcDetail);
					}
				}
			}
		}
	}
}